﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#include "GbmpUiViewMouseEventHandler.h"
#include "IUiViewMouseEventArgs.h"
#include "IApplication.h"
#include "IMainWindow.h"
#include "IStatusBar.h"
#include "IStatusBarPanel.h"
#include "IRibbon.h"
#include "IRibbonPage.h"
#include "ICommandButtonMenuDefinition.h"
#include "IControl.h"
#include "IControlDefinition.h"
#include "ILabelDefinition.h"
#include "GbmpControlIds.h"
#include "IActionManager.h"

#include "EnableCompileWarning_The_LAST_IncludeInCpp.h"
using namespace gcmp;

namespace
{
    void ChangePromptMessage()
    {
        IStatusBar* pStatusBar = IMainWindow::GetMainWindow()->GetStatusBar();
        DBG_WARN_AND_RETURN_VOID_UNLESS(pStatusBar, L"pStatusBar为空",L"GDMPLab",L"2024-03-30");

        IStatusBarPanel* pPanel = pStatusBar->GetPanel(ID_STATUS_BAR_PROMPT_PANEL);
        DBG_WARN_AND_RETURN_VOID_UNLESS(pPanel, L"pPanel为空",L"GDMPLab",L"2024-03-30");
        IControl* pLabelCtrl = pPanel->GetControl(ID_STATUS_BAR_PROMPT_LABEL);
        DBG_WARN_AND_RETURN_VOID_UNLESS(pLabelCtrl, L"pLabelCtrl为空",L"GDMPLab",L"2024-03-30");
        ILabelDefinition* pLabelDef = dynamic_cast<ILabelDefinition*>(pLabelCtrl->GetControlDefinition());
        DBG_WARN_AND_RETURN_VOID_UNLESS(pLabelDef, L"pLabelDef为空",L"GDMPLab",L"2024-03-30");

        std::wstring composedPromptMessages;
        std::vector<std::wstring> promptMessages = IActionManager::GetPromptMessages();
        FOR_EACH(promptMessage, promptMessages)
        {
            if (!promptMessage.empty())
            {
                if (!composedPromptMessages.empty())
                    composedPromptMessages += L" | ";
                composedPromptMessages += promptMessage;
            }
        }
        pLabelDef->SetCaption(composedPromptMessages);
    }
}

void GbmpUiViewMouseEventHandler::On(IUiViewMouseEventArgs* pArgs)
{
    DBG_WARN_AND_RETURN_VOID_UNLESS(pArgs, L"pArgs为空",L"GDMPLab",L"2024-03-30");

    if (pArgs->GetType() == UiViewMouseEventType::Move)
    {
        ChangePromptMessage();
    }
    else if (pArgs->GetType() == UiViewMouseEventType::Wheel)
    {
        int nDelta = pArgs->GetMouseWheelDelta();
        IRibbon* pRibbon = IMainWindow::GetMainWindow()->GetRibbon();
        if (pRibbon == nullptr) return;
        IRibbonPage* pPage = pRibbon->GetPage(L"gmExperimentPage");
        if (pPage == nullptr) return;
        IRibbonGroup* pGroup = pPage->GetGroup(L"4视图");
        if (pGroup == nullptr) return;
        IControl* pButtonControl = pGroup->GetControl(L"gbmpMenuUIViewMouseEvent");
        if (pButtonControl == nullptr) return;
        ICommandButtonMenuDefinition* pButton = dynamic_cast<ICommandButtonMenuDefinition*>(pButtonControl->GetControlDefinition());
        if (pButton == nullptr) return;

        std::wstring caption = StringUtil::FormatWString(L"UIViewMouseWheel成功-增幅%d", nDelta);
        pButton->SetCaption(caption);
    }
}

void gcmp::GbmpPreHighLightsChangeEventHandler::On(IPreHighlightsChangeEventArgs * pArgs)
{
    ChangePromptMessage();
}
